package com.zmovie.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmovie.framework.domain.Actor;
import com.zmovie.framework.domain.ActorMovie;
import com.zmovie.framework.domain.dto.ActorGroupDTO;
import com.zmovie.framework.service.ActorMovieService;
import com.zmovie.framework.mapper.ActorMovieMapper;
import com.zmovie.framework.service.ActorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【actor_movie】的数据库操作Service实现
* @createDate 2023-12-04 15:52:02
*/
@Service
public class ActorMovieServiceImpl extends ServiceImpl<ActorMovieMapper, ActorMovie>
    implements ActorMovieService{
    @Autowired
    private ActorMovieMapper actorMovieMapper;
    @Autowired
    private ActorService actorService;

    @Override
    public List<ActorMovie> listActorMovieByMovieId(Integer movieId) {
        LambdaQueryWrapper<ActorMovie> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActorMovie::getMovieId, movieId);
        wrapper.orderByAsc(ActorMovie::getActorId);
        return list(wrapper);
    }

    @Override
    public List<ActorGroupDTO> listActorByMovieIdGroupByRole(Integer movieId) {
        return actorMovieMapper.listActorByMovieIdGroupByRole(movieId);
    }

    @Override
    public List<Actor> listRelatedActorByMovieId(Integer movieId) {
        //已关联
        List<ActorMovie> actorMovies = listActorMovieByMovieId(movieId);
        //电影已关联角色id
        List<Integer> actorIds = actorMovies.stream().map(ActorMovie::getActorId).collect(Collectors.toList());
        return actorService.listByIds(actorIds);
    }

    @Override
    public List<Actor> listNotRelatedActorByMovieId(Integer movieId) {
        //已关联
        List<ActorMovie> actorMovies = listActorMovieByMovieId(movieId);
        if(actorMovies.size() == 0){
            //不存在关联的
            return actorService.list();
        }
        //电影已关联角色id
        List<Integer> actorIds = actorMovies.stream().map(ActorMovie::getActorId).collect(Collectors.toList());
        return actorService.listNotIn(actorIds);
    }

    @Override
    public List<Actor> listProtagonist(Integer movieId) {
        LambdaQueryWrapper<ActorMovie> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActorMovie::getMovieId, movieId);
        //演员
        wrapper.eq(ActorMovie::getActorRoleId, 1);
        wrapper.orderByAsc(ActorMovie::getActorId);
        wrapper.last(" limit 2");
        List<ActorMovie> actorMovieList = list(wrapper);
        List<Integer> actorIds = actorMovieList.stream().map(ActorMovie::getActorId).collect(Collectors.toList());
        if(actorIds.size() == 0){
            return new ArrayList<>();
        }
        return actorService.listByIds(actorIds);
    }
}




